// pages/address/index.js
 var util = require("../../utils/util.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addrList: '',
    carCount: 0
  },
  Navigation: function (event) {
    var that = this;
    app.Navigation(event, that);
  },
  initCart: function () {
    var _this = this
    util.djax({
      url: '/wxapp/Cart/getCartCount'
    }).then(function (res) {
      if (res.code != 1001) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        return
      }
      var cartNumber = res.data > 0 ? res.data : 0
      console.log(cartNumber)
      _this.setData({
        carCount: cartNumber
      })

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initCart()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getAddrList()
  },
  selAddr: function (e) {
   
    var route = wx.getStorageSync('fromWhere')
    wx.setStorageSync('fromWhere', null)
    if (route =='/pages/map/index') {
      wx.setStorageSync('myAddress', e.currentTarget.dataset.addr)
      wx.reLaunch({
        url: '/pages/index/index',
      })
    } else if (route == '/pages/order/index') {
      var addrList = this.data.addrList
      var id = e.currentTarget.dataset.id
      var addrInfo;
      for (var i=0; i<addrList.length; i++) {
        if (id == addrList[i].id) {
          addrInfo = addrList[i]
        }
      }
      wx.setStorageSync('orderAddress', addrInfo)
      wx.reLaunch({
        url: '/pages/order/index',
      })
    }
  },
  editAddr: function(e) {
    wx.redirectTo({
      url: 'add?id=' + e.currentTarget.dataset.id,
    })
  },
  delAddr: function(e) {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          var id = e.currentTarget.dataset.id
          util.djax({
            url: '/wxapp/Address/del',
            data: {
              id: id
            }
          }).then(function (res) {
            if (res.code != 1001) {
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
            } else {
              var address = wx.getStorageSync('orderAddress')
              if (address.id==id) {
                wx.setStorageSync('orderAddress','')
              }

              _this.getAddrList()
            }
          })
        }
      }
    })
  },
  getAddrList: function() {
    var _this = this
     util.djax({
       url: '/wxapp/Address/index'
     }).then(function(res){
        if (res.code!=1001) {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
        } else {
           _this.setData({
             addrList: res.data
           }) 
        }
     })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})